// pages/examineOnline/examineOnline.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    examineList: [],
    page: 1, //当前页数
    totil: 1, //总页数
    pageSize: 15,
    noMore: false,
    loading: false,
    cardIndex: -1, //操作数据的序号
    isSearch: false, //是否开启搜索
    start_time: '',
    end_time: '',
    audit_status: '',
    province: '',
    city: '',
    area: '',
    card: '',
    card_class: '',
    card_type: '',
    village_name: '',
    communityList: [],
    communityIndex: 999,
    isDisabled: false,
    statusList: [{
      id: 999,
      label: '全部'
    }, {
      id: 1,
      label: '审核中'
    }, {
      id: 2,
      label: '审核通过'
    }, {
      id: 3,
      label: '审核拒绝'
    }],
    statusIndex: 999,
    typeList: [{
      id: 999,
      label: '全部'
    }, {
      id: 0,
      label: '普通卡'
    }, {
      id: 1,
      label: '老年卡'
    }, {
      id: 2,
      label: '婴幼儿卡'
    }],
    typeIndex: 999,
    areaInfo_name: [],
    area_id_Info: [],
    empty: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.examineList();
  },


  onReady: function () {
    this.cascade = this.selectComponent("#cascade")
  },

  showCascade() {
    this.cascade.selection(this.data.area_id_Info);
  },

  cascadeComplete(e) {
    let {
      ids,
      names
    } = e.detail;
    this.setData({
      areaInfo_name: names,
      area_id_Info: ids,
      communityIndex: 999,
      communityList: [], //园区列表
    })
    this.getVillage();
  },



  /**
   * 页面上拉触底事件的处理函数
   */
  reachBottom: function () {
    var that = this;
    var page = that.data.page + 1;
    if (page > that.data.totil) {
      that.setData({
        noMore: true
      })
      return false;
    }
    that.setData({
      page: page
    })
    that.examineList();
  },

  // 打开详情
  openDetails: function (e) {
    wx.navigateTo({
      url: '../../examineOnlineDetails/examineOnlineDetails?id=' + e.currentTarget.dataset.id
    })
  },
  //获取申请列表
  examineList: function () {
    var that = this;
    let area_id_Info = that.data.area_id_Info;
    let audit_status = '',
      province = '',
      city = '',
      area = '',
      card_type = '',
      village_name = '';
    if (area_id_Info.length == 3) {
      province = area_id_Info[0];
      city = area_id_Info[1];
      area = area_id_Info[2];
    }
    if (that.data.statusIndex != 999 && that.data.statusIndex != 0) {
      audit_status = that.data.statusList[that.data.statusIndex].id;
    }
    if (that.data.typeIndex != 999 && that.data.typeIndex != 0) {
      card_type = that.data.typeList[that.data.typeIndex].id;
    }
    if (that.data.communityIndex != 999) {
      village_name = that.data.communityList[that.data.communityIndex].village_name;
    }

    app.ajax({
      url: 'Partner/WaterCard/accountAuditList_mobile',
      method: "POST",
      data: {
        page: that.data.page,
        row: that.data.pageSize,
        audit_status: audit_status,
        province: province,
        city: city,
        area: area,
        card: that.data.card,
        card_class: 2,
        start_time: that.data.start_time,
        card_type: card_type,
        end_time: that.data.end_time,
        village_name: village_name,
        type: 1
      },
      success: function (res) {
        wx.hideLoading();
        var flag = res.data;
        //console.log(flag);
        if (res.data.code == 1000) {
          let len = 0;
          for (let i = 0; i < flag.data.data.length; i++) {
            len += flag.data.data[i].child.length;
          }
          // console.log(len)
          if (that.data.pageSize > len) {
            that.setData({
              noMore: true
            })
          } else {
            that.setData({
              noMore: false
            })
          }

          if (flag.data.data.length != 0 && that.data.examineList.length != 0) {
            if (that.data.examineList[that.data.examineList.length - 1].date == flag.data.data[0].date) {
              var tmp = that.data.examineList;
              var same_time = tmp[tmp.length - 1].child.concat(flag.data.data[0].child);
              tmp[tmp.length - 1].child = same_time;
              that.setData({
                examineList: tmp
              });
              flag.data.data.splice(0, 1);
            }
          }
          var record = that.data.examineList.concat(flag.data.data)
          that.setData({
            examineList: record,
            totil: flag.data.page.page_count,
            isSearch: false,
            empty: flag.data.page.page_count == 0 ? true : false,
          });
        } else {
          // app.showToast('暂无数据');
          that.setData({
            noMore: true,
            isSearch: false
          })
        }
      },
      error: e => {
        wx.hideLoading();
      }
    })
  },
  //搜索
  search(e) {
    this.condition = {
      statusIndex: this.data.statusIndex,
      typeIndex: this.data.typeIndex,
      communityIndex: this.data.communityIndex,
      area_id_Info: this.data.area_id_Info,
      areaInfo_name: this.data.areaInfo_name,
      start_time: this.data.start_time,
      end_time: this.data.end_time,
      card: this.data.card
    }
    let isSearch = e.detail.isSearch;
    this.setData({
      isSearch: isSearch
    })
  },
  //点击搜索
  goSearch() {
    var that = this;
    that.setData({
      page: 1, //当前页数
      totil: 1, //总页数
      examineList: [],
      noMore: false,
      loading: false,
    })
    wx.showLoading();
    this.examineList();
  },
  //返回搜索
  goBack() {
    this.setData({
      isSearch: false,
      ...this.condition
    })
    delete(this.condition)
  },
  //卡号
  cardEdit(e) {
    this.setData({
      card: e.detail.value
    })
  },
  //选择审核状态
  statusChange(e) {
    this.setData({
      statusIndex: e.detail.value
    })
  },
  //选择水卡类型
  typeChange(e) {
    this.setData({
      typeIndex: e.detail.value
    })
  },
  //选择开始时间
  startTimeChange(e) {
    this.setData({
      start_time: e.detail.value
    })
  },
  //选择截止时间
  endTimeChange(e) {
    this.setData({
      end_time: e.detail.value
    })
  },

  //获取园区列表
  getVillage() {
    var that = this;
    app.ajax({
      url: 'Partner/Village/getAllVillage',
      data: {
        province: that.data.area_id_Info[0],
        city: that.data.area_id_Info[1],
        area: that.data.area_id_Info[2],
        village_name: '',
        get_type:1
      },
      success: function (res) {
        //console.log(res)
        if (res.data.code == 1000) {
          that.setData({
            communityList: res.data.data,
            isDisabled: false
          })
          if (res.data.data.length < 1) {
            app.showToast('该地区无园区,请重选地区');
            that.setData({
              communityIndex: 999,
              isDisabled: true
            })
          }
        } else {
          app.showToast(res.data.message);
        }
      }
    })
  },
  //选择园区
  communityChange(e) {
    this.setData({
      communityIndex: e.detail.value
    })
  },
})